package cn.project.lemontree.service.impl;

import cn.project.lemontree.bean.Administrator;
import cn.project.lemontree.bean.Child;
import cn.project.lemontree.bean.Class;
import cn.project.lemontree.bean.Parent;
import cn.project.lemontree.dao.AdministratorDao;
import cn.project.lemontree.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("administratorService")
public class AdministratorServiceimpl extends ServiceImpl<AdministratorDao, Administrator> implements AdministratorService {
    @Resource
    private AdministratorDao administratorDao;
    @Resource
    private ParentService parentService;
    @Resource
    private ChildService childService;
    @Resource
    private ClassService classService;
    @Resource
    private ParentChildService parentChildService;

    @Override
    public Administrator login(String telephone){
        return administratorDao.login(telephone);
    }

    @Override
    public String importExcel(MultipartFile file,Integer classId) throws IOException {
        InputStream inputStream = file.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Map<String,Parent> sp = new HashMap<>();
        Map<Parent , List<Child>> map = new HashMap<>();
        Integer count = 0;
        XSSFSheet sheet = workbook.getSheetAt(0);
        for(int i = 1;i <= sheet.getLastRowNum();i++){
            XSSFRow row = sheet.getRow(i);
             DataFormatter formatter = new DataFormatter();
            Child child = new Child();
            count++;
                child.setChildName(formatter.formatCellValue(sheet.getRow(i).getCell(0)));
                child.setCsex(formatter.formatCellValue(sheet.getRow(i).getCell(1)));
                child.setBirth(formatter.formatCellValue(sheet.getRow(i).getCell(2)));
                child.setHeight(formatter.formatCellValue(sheet.getRow(i).getCell(3)));
                child.setWeight(formatter.formatCellValue(sheet.getRow(i).getCell(4)));
                child.setAClass(new Class(classId));
            Parent parent = new Parent();
            String parentName = formatter.formatCellValue(sheet.getRow(i).getCell(5));
                parent.setParentName(parentName);
                parent.setTelephone(formatter.formatCellValue(sheet.getRow(i).getCell(6)));
                parent.setParentStatus(formatter.formatCellValue(sheet.getRow(i).getCell(7)));
                parent.setMembership(formatter.formatCellValue(sheet.getRow(i).getCell(8)));
            if(!sp.containsKey(parentName)){
                sp.put(parentName,parent);
                List<Child> list = new ArrayList<>();
                list.add(child);
                map.put(parent,list);
                continue;
            }
            map.get(sp.get(parentName)).add(child);
        }
        for(Parent parent : map.keySet()){
            parentService.save(parent);
            Parent p = parentService.findBytp(parent.getTelephone());
            for(Child child : map.get(parent)){
                childService.saveChild(child);
                Child c = childService.research(child.getChildName(),child.getBirth());
                parentChildService.save(p,c);
            }
        }
        Class c = classService.findClassByid(classId);
        c.setNum(c.getNum() + count);
        return "1";
    }

    @Override
    public List<Administrator> listPage(Integer page, Integer size, String userName, String telephone) {
        return administratorDao.listPage((page-1)*size,size,userName,telephone);
    }

    @Override
    public int count(String userName, String telephone) {
        return administratorDao.count(userName,telephone);
    }

    @Override
    public boolean save(Administrator administrator) {
        return administratorDao.save(administrator)>0;
    }

    @Override
    public boolean update(Administrator administrator) {
        return administratorDao.update(administrator)>0;
    }

}
